package capricornus.utils.blood
{
	import capricornus.components.utils.FilterUtils;
	import capricornus.core.ClassManager;
	
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	
	/**
	 * 处理多管血条 
	 * @author kael
	 * 
	 */	
	public class BloodMulti extends Sprite
	{
		private var _arrBloods:Array;
		private var _arrBloodsBack:Array;
		private var _arrMask:Array;
		
		private var _lastBM:Bitmap;
		
		private var _index:int;
		
		public function BloodMulti( arrClassName:Array )
		{
			super();
			init( arrClassName );
		}
		
		private function init( arrClassName:Array ):void
		{
			this.mouseChildren = false;
			
			_arrBloods = new Array();
			_arrMask = new Array();
			_arrBloodsBack = new Array();
			
			var bm:Bitmap;
			var mask:Shape;
			var w:int = 0;
			var h:int = 0;
			_index = -1;
			
			var s:Sprite;
			for( var i:int=0; i<arrClassName.length; i++ )
			{
				s = new Sprite();
				
				bm = ClassManager.getClassInstance( arrClassName[i] );
				bm.visible = false;
				s.addChild( bm );
				_arrBloods.push( bm );
				
				mask = new Shape();
				mask.graphics.beginFill(0);
				mask.graphics.drawRect( 0, 0, bm.width, bm.height );
				mask.graphics.endFill();
				
				bm.mask = mask;
				
				s.addChild( mask );
				_arrMask.push( mask );
				
				bm = new Bitmap( bm.bitmapData );
				bm.visible = false;
				bm.filters = FilterUtils.getShineFilter(40);
				s.addChildAt( bm, 0 );
				_arrBloodsBack.push( bm );
				
				addChild( s );
			}
			
			_lastBM = _arrBloodsBack[_arrBloodsBack.length-1];
		}
		
		/**
		 * 设置血条比例 
		 * @param value
		 * @param index 设置第几条血条，1索引。跟arrClassName对应
		 * 
		 */		
		public function setProportion( value:Number, index:int ):void
		{
			if( value < 0 ) value = 0;
			else if( value > 1 ) value = 1;
			
			if( index < 1 ) return;
			if( index > _arrBloods.length ) return;
			
			var i:int;
			if( _index != index )
			{
				_index = index;
				for( i=0; i<index; i++ )
				{
					(_arrBloods[i] as Bitmap).visible = true;
					(_arrBloodsBack[i] as Bitmap).visible = true;
				}
				for( ; i<_arrBloods.length; i++ )
				{
					(_arrBloods[i] as Bitmap).visible = false;
					(_arrBloodsBack[i] as Bitmap).visible = false;
				}
				index--;
				for( i=0; i<index; i++ )
				{
					(_arrMask[i] as Shape).scaleX = 1;
				}
				if( _lastBM ) TweenLite.killTweensOf( _lastBM );
				_lastBM = _arrBloodsBack[_index-1];
			}
			
			(_arrMask[_index-1] as Shape).scaleX = value;
			if( _lastBM.scaleX > value )
			{
				TweenLite.to( _lastBM, 1, {scaleX:value} );
			}
			else
			{
				TweenLite.killTweensOf( _lastBM );
				_lastBM.scaleX = value;
			}
		}
	}
}




















